package com.lcx.movie.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcx.movie.common.Rep;
import com.lcx.movie.entity.Orderinfo;
import com.lcx.movie.service.OrderinfoService;
import net.minidev.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
//徐大大
@CrossOrigin(origins = "*")
@RestController
public class OrderinfoController {
    @Autowired
    private OrderinfoService orderinfoService;
    // 分页模糊查询订单表(用户账号)
    @GetMapping("/selectOrderinfo/{page}")
    public Rep selectOrderinfoPage(@PathVariable("page")int page, @RequestParam(required = false)String userName){
        PageHelper.startPage(page,10);
        //执行查询
        List<Orderinfo> list=this.orderinfoService.selectOrderinfoPage(userName);
        PageInfo<Orderinfo>orderinfoPageInfo=new PageInfo<>(list);
        return new Rep().addData("orderinfoPageInfo",orderinfoPageInfo);
    }
    @PostMapping("/findOrderByUserId")
    // 根据用户id查询用户订单
    public JSONObject findOrderByUserId(@Param("userId") Integer userId) {
        List<Orderinfo> orderInfos = orderinfoService.findOrdersByUserId(userId);
        // 创建JSONObject对象
        JSONObject obj = new JSONObject();
        // 给obj对象赋值
        obj.put("data", orderInfos);
        return obj;
    }

    //查询个人订单
    @PostMapping("/findOrderByUserName")
    @ResponseBody
    public JSONObject findOrderByUserName(@RequestParam(value="page",defaultValue="1")Integer page, @RequestParam(value="limit",defaultValue="10")Integer limit, @RequestParam(value = "userId")String userId) {
        PageInfo<Orderinfo> info = orderinfoService.findOrdersByUserName(page, limit, userId);
        JSONObject obj = new JSONObject();
        obj.put("code", 0);
        obj.put("msg", "");
        obj.put("count", info.getTotal());
        obj.put("data", info.getList());
        return obj;
    }

    @GetMapping("/buyTickets")
    public void buyTickets(@RequestParam("schedule_id") long schedule_id, @RequestParam("position") String position, @RequestParam("price") int price, @RequestParam("seatId") String seatId, @RequestParam("userId") Integer userId, HttpServletResponse response) throws IOException {
        //生成订单号
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHMMssSSS");
        String orderId = format.format(date);
        //新增订单
        int order = orderinfoService.buyTickets(orderId, userId, schedule_id, position, price, seatId);
        if (order > 0) {
            System.out.println("支付");
            response.sendRedirect("alipay/pay?orderId=" + orderId);
        }
    }
    //查询订单
    @GetMapping("/getOrders")
    public JSONObject getOrders(@RequestParam(value = "userId") String userId) {
        PageInfo<Orderinfo> info =  orderinfoService.findOrdersByUser(1, 10, userId);
        JSONObject obj = new JSONObject();
        obj.put("code", 0);
        obj.put("msg", "");
        obj.put("count", info.getTotal());
        obj.put("data", info.getList());
        return obj;
    }
    //零食支付
    @GetMapping("/buyTicketsSnacks")
    public void buyTicketsSnacks(@RequestParam("schedule_id") long schedule_id, @RequestParam("position") String position, @RequestParam("price") int price, @RequestParam("snackId") String snackId, @RequestParam("userId") Integer userId, HttpServletResponse response) throws IOException {
        //生成订单号
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHMMssSSS");
        String orderId = format.format(date);
        System.out.println("orderId = " + orderId);
        System.out.println("schedule_id=" + schedule_id + "position=" + position + "price=" + price + "snackId=" + snackId + "userId=" + userId);
        //新增零食订单
        int order = orderinfoService.buyTicketsSnacks(orderId, userId, schedule_id, position, price, snackId);
        if (order > 0) {
            System.out.println("支付");
            response.sendRedirect("alipay/pay?orderId=" + orderId);
        }
    }
    /**
     * 影院管理员查看订单信息
     * @param cinemaId 影院id
     * @return 订单集合
     */
    @GetMapping("/selectOrderInfoByCinemaId")
    public Rep selectOrderInfoByCinemaId(@Param("page") Integer page,@Param("pageSize") Integer pageSize
                                        ,@Param("cinemaId") Integer cinemaId
                                        ,@Param("orderState") Integer orderState){
        // 开启分页
        PageHelper.startPage(page,pageSize);
        // 执行查询
        List<Orderinfo> orderinfoList = this.orderinfoService.selectOrderInfoByCinemaId(cinemaId,orderState);
        // 创建分页对象
        PageInfo<Orderinfo> orderinfoPageInfo = new PageInfo<>(orderinfoList);
        // 返回
        return new Rep().addData("orderinfoPageInfo",orderinfoPageInfo);
    }
    /**
     * 同意退票方法
     * @param orderId 订单id
     * @return 受影响的行数
     */
    @GetMapping("/agreeRefund")
    public String agreeRefund(@Param("orderId") String orderId){
        int agreeRefund = this.orderinfoService.agreeRefund(orderId);
        if (agreeRefund>0){
            return "agree success";
        }else {
            return "agree fail";
        }
    }

    /**
     * 根据影院id查询该影院的订单数量
     * @param cinemaId 影院id
     * @return 订单数量
     */
    @GetMapping("/getCinemaManageOrder")
    public int getCinemaManageOrder(@Param("cinemaId") Integer cinemaId){
        int cinemaManageOrder = orderinfoService.getCinemaManageOrder(cinemaId);
        System.out.println("cinemaManageOrder = " + cinemaManageOrder);
        return  cinemaManageOrder;
    }

}
